package com.ruoyi.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.BizPlan;
import com.ruoyi.business.domain.BizUser;
import com.ruoyi.common.db.BaseService;
import net.sf.ehcache.search.query.QueryManager;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <pre>
 *
 * </pre>
 *
 * @author zhangli
 * @since 2023/10/22
 */
@Component
public class BizPlanDataService extends BaseService<Integer, BizPlan> {


    public BizPlan getByWeekNum(int weekNum,String className) {
        if(weekNum == 0) {
            QueryWrapper<BizPlan> queryWrapper = new QueryWrapper<>();
            LocalDate now = LocalDate.now();
            queryWrapper.ge("week_end", now);
            queryWrapper.le("week_start", now);
            queryWrapper.eq("class_name", className);

            return findOne(queryWrapper);
        }else if(weekNum > 0){
            QueryWrapper<BizPlan> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("week_num",weekNum);
            queryWrapper.eq("class_name", className);

            return findOne(queryWrapper);
        }else{
            return null;
        }
    }

    public int getMaxWeekNum(){

            QueryWrapper<BizPlan> queryWrapper = new QueryWrapper<>();
            LocalDate now = LocalDate.now();
            queryWrapper.orderByDesc("week_num");
            queryWrapper.last("limit 1");

            return findOne(queryWrapper).getWeekNum();

    }

    public double getBaseScore(String className){
        QueryWrapper<BizPlan> queryWrapper = new QueryWrapper<>();
        LocalDate now = LocalDate.now();
        queryWrapper.le("week_start", now);
        queryWrapper.eq("class_name", className);

        List<BizPlan> bizPlans = selectList(queryWrapper);

        if(bizPlans.isEmpty()) {
            return 1.0;
        }

        return bizPlans.stream()
                .filter(one->Objects.nonNull(one.getScoreSummary()))
                .mapToDouble(BizPlan::getScoreSummary)
                .sum();
    }
}
